<?php
namespace user\service;

use herosphp\bean\Beans;
use herosphp\core\Log;
use herosphp\string\StringUtils;
use herosphp\utils\ArrayUtils;
use payment\models\SignVo;
use payment\utils\SerialNumberUtils;
use user\service\interfaces\IUserService;
use common\service\CommonService;
use herosphp\core\Loader;

Loader::import('user.service.interfaces.IUserService');

/**
 * user(Service)接口实现
 * @package user\service
 * @author yangjian<yangjian102621@gmail.com>
 */
class UserService extends CommonService implements IUserService {

    /**
     * 签约银行
     * @param $userid
     * @param $data
     * @return mixed
     */
    public function signBank($userid, $data)
    {
        //获取银行支付通道和银行信息
        $payWayService = Beans::get('payment.payway.service');
        $bankService = Beans::get('payment.bank.service');
        $payway = $payWayService->getItem($data['payway']);
        $bank = $bankService->getItem($data['sign_bank_id']);
        $user = $this->getItem($userid);

        if ( empty($user) ) return false;

        //发送签约信息到银行
        $signVo = new SignVo();
        $signVo->setBankName($payway['key']); //支付通道
        $signVo->setSubAccountName($user['virtual_name']); //虚拟帐号户名
        $signVo->setContactName($user['contacter']); //联系人
        $signVo->setContactPhone($user['username']); //联系电话
        $signVo->setRequestNumber(SerialNumberUtils::generate($payway['serial_prefix'])); //流水号
        $signVo->setRemark('广发注册接口');
        $signVo->setType(SignVo::SIGN);

        $paymentService = Beans::get('payment.payment.service');
        $res = $paymentService->sign($signVo);
        if ( $res === false ) return false;

        //更新签约信息
        $signData = $data;
        $signData['virtual_account'] = $res['subAccount'];

        $signData['sign_time'] = date("Y-m-d H:i:s");
        $signData['sign_status'] = self::SIGN_STATUS_SIGNED;
        $this->beginTransaction();
        $result = $this->update($signData, $userid);

        $user = $this->getItem($userid);
        //添加签约日志
        if ( $result != false ) {
            $logData = $data;
            $logData['sign_type'] = self::OPT_SIGN;
            $logData['userid'] = $userid;
            $logData['mobile'] = $user['username'];
            $logData['addtime'] = date("Y-m-d H:i:s");
            $ext['payway_name'] = $payway['name'];
            $logData['virtual_account'] = $res['subAccount'];
            $ext['sign_bank_branch'] = $data['sign_bank_branch'];
            $ext['bank_name'] = $bank['name'];
            $logData['acc_name'] = $user['virtual_name'];
            $logData['ext'] = StringUtils::jsonEncode($ext);

            $userSignLogService = Beans::get('user.bankSignLog.service');
            if ( $userSignLogService->add($logData) ) {
                $this->commit();
                return true;
            } else {
                $this->rollback();
                return false;
            }
        }
        return false;
    }

    /**
     * 银行解约
     * @param $userid
     * @return mixed
     */
    public function unsignBank($userid)
    {
        $user = $this->getItem($userid);
        if ( empty($user) ) return false;

        //获取银行支付通道和银行信息
        $payWayService = Beans::get('payment.payway.service');
        $bankService = Beans::get('payment.bank.service');
        $payway = $payWayService->getItem($user['payway']);
        $bank = $bankService->getItem($user['sign_bank_id']);

        //向银行发送解约请求
        $signVo = new SignVo();
        $signVo->setBankName($payway['key']); //支付通道
        $signVo->setSubAccountName($user['virtual_name']); //虚拟帐号户名
        $signVo->setSubAccount($user['virtual_account']); //虚拟帐号
        $signVo->setContactName($user['name']); //联系人
        $signVo->setContactPhone($user['username']); //联系电话
        $signVo->setRequestNumber(SerialNumberUtils::generate($payway['serial_prefix'])); //流水号
        $signVo->setRemark('广发解约接口');
        $signVo->setType(SignVo::UNSIGN);

        $paymentService = Beans::get('payment.payment.service');
        $res = $paymentService->sign($signVo);
        if ( $res === false ) return false;

        //1.更改签约状态
        $data['sign_status'] = self::SIGN_STATUS_DEL;
        $data['virtual_account'] = '';
        $data['payway'] = 0;
        $data['card_no'] = '';
        $data['sign_bank_id'] = 0;
        $data['sign_bank_branch'] = '';
        $data['sign_bank_branch'] = '';
        $data['sign_bank_code'] = '';
        $data['sign_time'] = null;
        $data['unsign_time'] = date("Y-m-d H:i:s");

        $this->beginTransaction();
        if ( $this->update($data, $userid) ) {
            //2.添加解约日志
            $logData['sign_type'] = self::OPT_UNSIGN;
            $logData['userid'] = $userid;
            $logData['mobile'] = $user['username'];
            $logData['acc_name'] = $user['virtual_name']; //户名
            $logData['payway'] = $user['payway'];
            $logData['card_no'] = $user['card_no'];
            $logData['virtual_account'] = $user['virtual_account'];
            $logData['sign_bank_id'] = $user['sign_bank_id'];
            $ext['payway_name'] = $payway['name'];
            $ext['sign_bank_branch'] = $user['sign_bank_branch'];
            $ext['bank_name'] = $bank['name'];
            $logData['ext'] = StringUtils::jsonEncode($ext);
            $logData['addtime'] = date("Y-m-d H:i:s");

            $userSignLogService = Beans::get('user.bankSignLog.service');
            if ( $userSignLogService->add($logData) ) {
                $this->commit();
                return true;
            } else {
                $this->rollback();
                return false;
            }
        }
        return false;

    }

    public function getItem($conditions, $fields, $order)
    {
        $item = parent::getItem($conditions, $fields, $order);
        if ( !empty($item) && $item['type'] == self::USER_TYPE_COMPANY ) {
            $model = Loader::model("Company");
            $company = $model->getItem($item['id']);
            $item['company'] = $company;
        }

        return $item;
    }
}
